package com.ap.core.validation.message.confirmpayment;

import com.ap.core.common.types.MessageStatus;
import com.ap.core.cxf.jaxb.common.CurrencyCodeType;
import com.ap.core.cxf.jaxb.paymentdetails.TransactionStatusCode;
import com.ap.core.cxf.jaxb.paymentdetails.TxnStatusType;
import com.ap.core.domain.OrderPayment;
import com.ap.core.domain.QuickPayDeliveryAddress;
import com.ap.core.message.managepayment.info.ConfirmPaymentRequestMessageInfo;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.joda.time.LocalDateTime;

import java.math.BigDecimal;

/**
 * Confirm Payment Message
 */
public class ConfirmPaymentMessage {

    private String apTransactionId;
    private String apRetrievalTransactionId;
    private String fasterPaymentsRef;
    private String quickPayAddressLine1;
    private String quickPayAddressLine2;
    private String quickPayAddressLine3;
    private String quickPayAddressLine4;
    private String quickPayAddressLine5;
    private String quickPayAddressLine6;
    private String postCode;
    private String countryCode;
    private String fiId;
    private String acctNum;
    private String acctSortCode;
    private String rollNum;
    private TxnStatusType txnStatus;
    private TransactionStatusCode txnStatusCode;
    private BigDecimal totalPaymentReqAmount;
    private CurrencyCodeType currency;
    private LocalDateTime pymtDecTimestamp;

    public ConfirmPaymentMessage(ConfirmPaymentRequestMessageInfo confirmPaymentRequestMessageInfo){
        this.apTransactionId = confirmPaymentRequestMessageInfo.getPaymtDetails().getApTransactionId();
        this.apRetrievalTransactionId = confirmPaymentRequestMessageInfo.getPaymtDetails().getApTRId();
        this.fasterPaymentsRef = confirmPaymentRequestMessageInfo.getPaymtDetails().getFasterPaymentsRef();
        this.txnStatus = confirmPaymentRequestMessageInfo.getPaymtDetails().getPaymentStatusInfo().getTxnStatus();
        this.txnStatusCode = confirmPaymentRequestMessageInfo.getPaymtDetails().getPaymentStatusInfo().getTxnStatusCode();
        this.totalPaymentReqAmount = confirmPaymentRequestMessageInfo.getPaymtDetails().getPaymentStatusInfo().getTotalPaymentReqAmount();
        this.currency = confirmPaymentRequestMessageInfo.getPaymtDetails().getPaymentStatusInfo().getCurrency();
        this.pymtDecTimestamp = confirmPaymentRequestMessageInfo.getPaymtDetails().getPaymentStatusInfo().getPymtDecTimestamp();
        if(null != confirmPaymentRequestMessageInfo.getPaymtDetails().getQkPayDelAddress()){
            this.quickPayAddressLine1 =  confirmPaymentRequestMessageInfo.getPaymtDetails().getQkPayDelAddress().getAddressLine1();
            this.quickPayAddressLine2 =  confirmPaymentRequestMessageInfo.getPaymtDetails().getQkPayDelAddress().getAddressLine2();
            this.quickPayAddressLine3 =  confirmPaymentRequestMessageInfo.getPaymtDetails().getQkPayDelAddress().getAddressLine3();
            this.quickPayAddressLine4 =  confirmPaymentRequestMessageInfo.getPaymtDetails().getQkPayDelAddress().getAddressLine4();
            this.quickPayAddressLine5 =  confirmPaymentRequestMessageInfo.getPaymtDetails().getQkPayDelAddress().getAddressLine5();
            this.quickPayAddressLine6 =  confirmPaymentRequestMessageInfo.getPaymtDetails().getQkPayDelAddress().getAddressLine6();
            this.postCode = confirmPaymentRequestMessageInfo.getPaymtDetails().getQkPayDelAddress().getPostCode();
            this.countryCode = confirmPaymentRequestMessageInfo.getPaymtDetails().getQkPayDelAddress().getCountryCode();
        }
        this.fiId = confirmPaymentRequestMessageInfo.getConsumerAcct().getFIId();
        this.acctNum = confirmPaymentRequestMessageInfo.getConsumerAcct().getAcctNum();
        this.acctSortCode = confirmPaymentRequestMessageInfo.getConsumerAcct().getAcctSortCode();
        this.rollNum = confirmPaymentRequestMessageInfo.getConsumerAcct().getRollNum();

    }

    public ConfirmPaymentMessage(OrderPayment orderPayment, QuickPayDeliveryAddress quickPayDeliveryAddress, String fiId){
        this.apTransactionId = orderPayment.getId();
        this.apRetrievalTransactionId = orderPayment.getApTransactionRetrievalId().toString();
        this.fasterPaymentsRef = orderPayment.getFasterPaymentRef();
        this.txnStatus = orderPayment.getTransactionStatus();
        this.txnStatusCode = TransactionStatusCode.fromValue(orderPayment.getTransactionStatusCode());
        this.totalPaymentReqAmount = orderPayment.getTotalPaymentRequestAmount().getValue();
        this.currency = CurrencyCodeType.valueOf(orderPayment.getCurrencyCode());
        this.pymtDecTimestamp = orderPayment.getPaymentDescTimeStamp();
        if(null != quickPayDeliveryAddress){
            this.quickPayAddressLine1 =  quickPayDeliveryAddress.getAddressLine1();
            this.quickPayAddressLine2 =  quickPayDeliveryAddress.getAddressLine2();
            this.quickPayAddressLine3 =  quickPayDeliveryAddress.getAddressLine3();
            this.quickPayAddressLine4 =  quickPayDeliveryAddress.getAddressLine4();
            this.quickPayAddressLine5 =  quickPayDeliveryAddress.getAddressLine5();
            this.quickPayAddressLine6 =  quickPayDeliveryAddress.getAddressLine6();
            this.postCode = quickPayDeliveryAddress.getPostCode();
            this.countryCode = quickPayDeliveryAddress.getCountryCode();
        }
        this.fiId = fiId;
        this.acctNum = orderPayment.getDebitAccountNumber().toString();
        this.acctSortCode = orderPayment.getDebitSortCode().toString();
        this.rollNum = null == orderPayment.getRollNumber() ? null : orderPayment.getRollNumber().toString();

    }

    public String getAcctNum() {
        return acctNum;
    }

    public String getAcctSortCode() {
        return acctSortCode;
    }

    public String getApRetrievalTransactionId() {
        return apRetrievalTransactionId;
    }

    public String getApTransactionId() {
        return apTransactionId;
    }

    public String getCountryCode() {
        return countryCode;
    }

    public String getFasterPaymentsRef() {
        return fasterPaymentsRef;
    }

    public String getFiId() {
        return fiId;
    }

    public String getPostCode() {
        return postCode;
    }

    public String getQuickPayAddressLine1() {
        return quickPayAddressLine1;
    }

    public String getQuickPayAddressLine2() {
        return quickPayAddressLine2;
    }

    public String getQuickPayAddressLine3() {
        return quickPayAddressLine3;
    }

    public String getQuickPayAddressLine4() {
        return quickPayAddressLine4;
    }

    public String getQuickPayAddressLine5() {
        return quickPayAddressLine5;
    }

    public String getQuickPayAddressLine6() {
        return quickPayAddressLine6;
    }

    public String getRollNum() {
        return rollNum;
    }

    public CurrencyCodeType getCurrency() {
        return currency;
    }

    public LocalDateTime getPymtDecTimestamp() {
        return pymtDecTimestamp;
    }

    public BigDecimal getTotalPaymentReqAmount() {
        return totalPaymentReqAmount;
    }

    public TxnStatusType getTxnStatus() {
        return txnStatus;
    }

    public TransactionStatusCode getTxnStatusCode() {
        return txnStatusCode;
    }

    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this,obj);
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }
}
